#include <iostream>

using namespace std;

const int N = 1010, INF = 0x3f3f3f3f;
int f[N][N], a[N][N], ti[N][N]; 
int n, m, t;

int main()
{
	cin >> n >> m;
	for(int i = 1; i <= n; i++)
		for(int j = 1; j <= m; j++)
		{
			cin >> a[i][j];
		}
	
	for(int i = 0; i <= n; i++)
		for(int j = 0; j <= m; j++)
		{
			f[i][j] = -INF;
			ti[i][j] = INF;
		}
	cin >> t;
	for(int i = 1; i <= t; i++)
	{
		int x, y, v; cin >> x >> y >> v;
		ti[x][y] = v;
	}
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
		{
			if(i + j - 1 - 1 >= ti[i][j]) a[i][j] = -INF; 
		}
	}
	f[0][1] = 0;
	int ret = -INF;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= m; j++)
		{
			f[i][j] = max(f[i][j - 1], f[i - 1][j]) + a[i][j];
			ret = max(ret, f[i][j]);
		}
	}
	
	cout << ret << endl;
	return 0;
}
